package me.helllp.bootman.back.logic.system.services.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import me.helllp.bootman.back.enums.UserStatusEnum;
import me.helllp.bootman.back.enums.UserTypeEnum;
import me.helllp.bootman.back.logic.system.services.DataService;
import me.helllp.bootman.back.mapper.SusrPermissMapper;
import me.helllp.bootman.back.mapper.SusrRoleMapper;
import me.helllp.bootman.back.mapper.SusrRolePermissMapper;
import me.helllp.bootman.back.mapper.SusrUserMapper;
import me.helllp.bootman.back.mapper.SusrUserRoleMapper;
import me.helllp.bootman.back.model.SusrPermiss;
import me.helllp.bootman.back.model.SusrRole;
import me.helllp.bootman.back.model.SusrRolePermiss;
import me.helllp.bootman.back.model.SusrUser;
import me.helllp.bootman.back.model.SusrUserRole;
import me.helllp.bootman.common.services.IdService;
import me.helllp.bootman.common.utils.PasswordHelper;

@Service
public class DataServiceImpl implements DataService{

	@Autowired
	private SusrUserMapper userMapper;
	
	@Autowired
	private SusrPermissMapper permissMapper;
	
	@Autowired
	private SusrRoleMapper roleMapper;
	
	@Autowired
	private SusrRolePermissMapper rolePermMapper;
	
	@Autowired
	private SusrUserRoleMapper userRoleMapper;
	
	@Autowired
	private IdService idService;
	
	@Override
	public void createData() {
		/**
		 * 1. 创建用户
		 * 2. 创建权限
		 * 3. 创建角色
		 * 4. 创建关联关系
		 */
		
		//	创建权限
		SusrPermiss permissBean = new SusrPermiss();
		
		permissBean.setId(idService.nextId());
		permissBean.setPermissionName("systemAdmin");
		permissBean.setPermissionMemo("系统管理员");
		
		permissBean.setCreateTime(0L);
		permissBean.setCreateUserId(0L);
		permissBean.setUpdateTime(0L);
		permissBean.setUpdateUserId(0L);		
		
		permissMapper.insert(permissBean);

		//	创建用户
		SusrUser userBean = new SusrUser();
		
		userBean.setId(idService.nextId());
		userBean.setTelephone("13000000000");
		userBean.setUserStatus(UserStatusEnum.正常用户.getValue());
		userBean.setUserType(UserTypeEnum.系统用户.getValue());
		userBean.setSalt(PasswordHelper.generatorSalt());
		userBean.setPassword(PasswordHelper.encryptPassword("111111", userBean.getSalt()));

		userBean.setCreateTime(0L);
		userBean.setCreateUserId(0L);
		userBean.setUpdateTime(0L);
		userBean.setUpdateUserId(0L);	
		
		userMapper.insert(userBean);
		
		//	创建角色
		SusrRole roleBean = new SusrRole();
		
		roleBean.setId(idService.nextId());
		roleBean.setRoleName("系统管理员");
		roleBean.setRemark("拥有系统管理权限");
		roleBean.setCreateTime(0L);
		roleBean.setCreateUserId(0L);
		roleBean.setUpdateTime(0L);
		roleBean.setUpdateUserId(0L);	
		
		roleMapper.insert(roleBean);

		//	创建关联关系
		SusrUserRole userRoleBean = new SusrUserRole();
		
		userRoleBean.setId(idService.nextId());
		userRoleBean.setUserId(userBean.getId());
		userRoleBean.setRoleId(roleBean.getId());
		userRoleBean.setCreateTime(0L);
		userRoleBean.setCreateUserId(0L);
		userRoleBean.setUpdateTime(0L);
		userRoleBean.setUpdateUserId(0L);
		
		userRoleMapper.insert(userRoleBean);
		
		SusrRolePermiss rolePermissBean = new SusrRolePermiss();
		
		rolePermissBean.setId(idService.nextId());
		rolePermissBean.setPermissId(permissBean.getId());
		rolePermissBean.setRoleId(roleBean.getId());
		
		rolePermissBean.setCreateTime(0L);
		rolePermissBean.setCreateUserId(0L);
		rolePermissBean.setUpdateTime(0L);
		rolePermissBean.setUpdateUserId(0L);
		
		rolePermMapper.insert(rolePermissBean);
	}
	
}
